const { Service } = require("uni-cloud-router");
const uniID = require('uni-id');
const response=require("../utils/response.js");
const db = uniCloud.database({
	spaceId: 'b33db069-91a2-42ff-a831-31fbd5a04b97'
});
const dbCmd = db.command;
const collection=db.collection("opendb-banner");
module.exports = class BannersService extends Service {
  async add(data){
  	const res=await collection.add(data);
		if(res.id){
			return response.success(res);
		}else{
			return response.fail('添加失败');
		}
  }
	async edit(id,data){
		const res=await collection.doc(id).update(data);
		if(res.updated>0){
			return response.success(res);
		}else{
			return response.fail('修改失败');
		}
	}
	async getOne(id){
		const res=await collection.doc(id).get();
		if(res.data.length>0){
			return response.success(res.data[0]);
		}else{
			return response.fail('添加失败');
		}
	}
  async list(id){
  	const res=await collection.where({
			category_id:id
		}).get();
		if(res.affectedDocs>=0){
			return response.success(res.data);
		}else{
			return response.fail('添加失败');
		}
  }
  async delete(ids){
  	return await collection.where({
			a: dbCmd.in(ids)
		}).remove();
  }
};